package com.fau.wm.action.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalMedicine;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 药品管理Action
 * @author kangyj
 * @datetime 2014年7月27日,上午11:10:57
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class MedicineAction extends PublicAction implements ModelDriven<MedicalMedicine> {

	private static final long serialVersionUID = -2611260617584966156L;
	
	private MedicalMedicine medicine = new MedicalMedicine();
	
	/**
	 * @desc 查询药品所在位置
	 * @return null
	 * @author liy
	 * @datetime 2014年12月22日,下午4:09:41
	 */
	@Action(value = "medicine_location")
	public String medicineLocation(){
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getMedicineService().findMedicalLocation(medicine.getId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 设置特定药品的提醒数
	 * @return null
	 * @author liy
	 * @datetime 2014年12月21日,下午6:45:30
	 */
	@Action(value = "medicine_warn_spe")
	public String medicineWarnSpe(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.baseAllService.getMedicineService().setMedicineWarnNumSpe(medicine);
		map = new HashMap<String, Object>();
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 修改特定药品的提醒数
	 * @return null
	 * @author liy
	 * @datetime 2014年12月22日,上午10:08:25
	 */
	@Action(value = "medicine_warn_spe_edit")
	public String medicineWarnSpeEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.baseAllService.getMedicineService().updateMedicineWarnNumSpe(medicine);
		map = new HashMap<String, Object>();
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 设置全局缺货提醒数
	 * @return null
	 * @author liy
	 * @datetime 2014年12月21日,下午4:34:23
	 */
	@Action(value = "medicine_warn_num_set")
	public String medicineWarnNumSet(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.baseAllService.getMedicineService().setMedicineWarnNum(medicine.getWarnNum());
		map = new HashMap<String, Object>();
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 获得已设置的缺货提醒数
	 * @return null
	 * @author liy
	 * @datetime 2014年12月21日,下午4:02:22
	 */
	@Action(value = "medicine_warn_num")
	public String medicineWarnNum(){
		HttpServletResponse response = ServletActionContext.getResponse();
		int num = this.baseAllService.getMedicineService().findMedicineWarnNum();
		map = new HashMap<String, Object>();
		map.put("num", num);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 判断药品名称是否重复
	 * @return null
	 * @author liy
	 * @datetime 2014年12月18日,下午4:01:57
	 */
	@Action(value = "medicine_name_repeat")
	public String nameRepeat(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicine.getName())){
			boolean flag = this.baseAllService.getMedicineService().findMedicineByName(medicine.getName());
			map.put("result", flag);
			formatJSON(response, map);
		}
		return null;
	}
	
	/**
	 * @desc 转到药品查看详情页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月13日,上午10:06:53
	 */
	@Action(value = "medicine_see_details", results = {@Result(name = SUCCESS, location = "medicine_see_details.jsp")})
	public String see(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入药品位置查看页面
	 * @return String
	 * @author liy
	 * @datetime 2014年12月22日,下午3:50:10
	 */
	@Action(value = "medicine_view_location", results = {@Result(name = SUCCESS, location = "medicine_view_location.jsp")})
	public String location(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入具体药品缺货设置页面
	 * @return String
	 * @author liy
	 * @datetime 2014年12月21日,下午4:56:43
	 */
	@Action(value = "stockout_medicine_warn", results = {@Result(name = SUCCESS, location = "stockout_medicine_warn.jsp")})
	public String medicineWarn(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入具体药品缺货修改页面
	 * @return String
	 * @author liy
	 * @datetime 2014年12月22日,上午9:43:51
	 */
	@Action(value = "stockout_medicine_warn_edit", results = {@Result(name = SUCCESS, location = "stockout_medicine_warn_edit.jsp")})
	public String medicineWarnEdit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入药品缺货页面
	 * @return String
	 * @author liy
	 * @datetime 2014年12月21日,上午11:46:49
	 */
	@Action(value = "stockout_warn", results = {@Result(name = SUCCESS, location = "stockout_warn.jsp")})
	public String stockoutWarn(){
		return SUCCESS;
	}

	/**
	 * @desc 转到药品管理页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月27日,下午12:06:53
	 */
	@Action(value = "medicine_init", results = {@Result(name = SUCCESS, location = "medicine_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载药品列表数据
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月30日,下午4:48:14
	 */
	@Action(value = "medicine_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		medicine.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getMedicineService().findMedicineByPage(medicine, map);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询已设置提醒数的药品
	 * @return null
	 * @author liy
	 * @datetime 2014年12月21日,下午6:06:33
	 */
	@Action(value = "medicine_is_warn")
	public String medicineIsWarn(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		medicine.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getMedicineService().findMedicineIsWarn(medicine, map);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 添加药品
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月27日,下午1:05:37
	 */
	@Action(value = "medicine_add", results = {@Result(name = SUCCESS, location = "medicine_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 编辑药品
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月30日,下午9:40:03
	 */
	@Action(value = "medicine_edit", results = {@Result(name = SUCCESS, location = "medicine_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询药品信息
	 * @return
	 * @author suny
	 * @datetime 2014年9月11日,下午11:20:22
	 */
	@Action(value = "medicine_find")
	public String findMedicineById(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicine.getId())){
			medicine = this.baseAllService.getMedicineService().findMedicineById(medicine.getId());
			map.put("result", medicine);
		}else{
			map.put("message", "error");
			}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 编辑保存药品
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月30日,下午9:41:54
	 */
	@Action(value = "medicine_edit_save")
	public String editSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicine.getId())){
			this.baseAllService.getMedicineService().saveEditMedicine(medicine);
			map.put("success", true);
		}else{
			map.put("message", "error");
			}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 选择药品类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月28日,上午10:53:38
	 */
	@Action(value = "medicine_select_sort", results = {@Result(name = SUCCESS, location = "medicine_select_sort.jsp")})
	public String selectMedicineSort(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存药品
	 * @return null
	 * @author kangyj
	 * @datetime 2014年7月28日,上午11:59:51
	 */
	@Action(value = "medicine_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Boolean flag = this.baseAllService.getMedicineService().saveMedicine(medicine, admin);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除药品
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月30日,下午4:49:04
	 */
	@Action(value = "medicine_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicine.getId())){
			boolean flag = this.baseAllService.getMedicineService().delMedicine(medicine);
			map.put("success", flag);
		}else{
			map.put("message", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量删除药品提醒数
	 * @return null
	 * @author liy
	 * @datetime 2014年12月22日,上午9:27:08
	 */
	@Action(value = "medicine_del_warn")
	public String delWarn(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicine.getId())){
			this.baseAllService.getMedicineService().delMedicineWarnNum(medicine);
			map.put("success", true);
		}else{
			map.put("message", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 验证输入编码是否已存在
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月31日,上午11:35:10
	 */
	@Action(value = "medicine_ver_serialnum")
	public String verSerialNum(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicine.getSerialNum())){
			Boolean flag = this.baseAllService.getMedicineService().verSerialNum(medicine.getSerialNum());
			if(flag){
				map.put("result", true);
			}else{
				map.put("result", false);
			}
		}else{
			map.put("message", "error");
			}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 打开导入页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月6日,上午11:36:29
	 */
	@Action(value = "medicine_import_add", results = {@Result(name = SUCCESS, location = "medicine_import_add.jsp")})
	public String importAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 导入Excel
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月30日,下午2:13:17
	 */
	@Action(value = "medicine_import_save")
	public String importSave() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try {
			File file = this.getImportFile(request);
			String info = this.baseAllService.getMedicineService().importMedicine(admin, file);
			if(info.equals("true")) {
				map.put("success", "true");
			} else if(info.equals("false")) {
				map.put("success", "false");
			} else {
				map.put("success", info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 下载药品导入模版
	 * @return null
	 * @author zhaoey
	 * @datetime 2015年4月3日,上午10:39:07
	 */
	@Action(value = "medicine_download")
	public String downloadMedicineTemp() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			response.addHeader("Content-Disposition", "attachment;filename=" + com.fau.wm.util.FileNameEncoderUtil.encode(request,"导入药品模板.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMedicineService().downloadMedicineTemp(user);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public MedicalMedicine getModel() {
		return medicine;
	}

}
